<template>
  <div class="app-container">
    <!-- 放置表单 -->
    <el-form ref="userForm" label-width="60px" :model="userInfo" :rules="rules">
      <!-- label 表示是input前的文字描述 -->
      <el-form-item label="姓名:" prop="username">
        <!-- v-model  双向绑定 -->
        <el-input v-model="userInfo.username" style="width: 300px" />
      </el-form-item>
      <el-form-item label="密码:">
        <el-input
          v-model="userInfo.password"
          style="width: 300px"
          type="password"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="updataUsername">更新</el-button>
        <el-button @click="$router.back()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { saveUserDetailById } from '@/api/employees'
export default {
  props: {
    userInfo: {
      type: Object,
      default: () => ({})
    }
  },
  data () {
    return {
      rules: {
        username: [{ required: true, min: 3, max: 10, message: '用户名必填！', trigger: 'change' }]
      }
    }
  },
  methods: {
    async updataUsername () {
      try {
        await this.$refs.userForm.validate()
        await saveUserDetailById(this.userInfo)
        this.$message.success('更改成功')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
